package com.tifa.vote.dao.impl;

import com.tifa.vote.bean.User;
import com.tifa.vote.bean.Vote;
import com.tifa.vote.bean.VoteNums;
import com.tifa.vote.dao.VoteDao;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

/**
 * @Classname VoteDaoImpl
 * @Description
 * @Date 2022/6/15 15:18
 * @Created by hengyuan
 */
@Repository
public class VoteDaoImpl implements VoteDao {

  @Autowired
  private NamedParameterJdbcTemplate jdbcTemplate;

  @Override
  public List<Vote> getVotes() {
    String sql = "select * from vote_item";
    RowMapper<Vote> rm = BeanPropertyRowMapper.newInstance(Vote.class);
    List<Vote> query = jdbcTemplate.query(sql, rm);
    return query;
  }

  @Override
  public Vote getVoteById(Integer id) {
    String sql = "select title,description,single from vote_item where id=:id";
    String anotherSql = "select id,nums,item from vote_detail where vote_item_id=:vote_item_id ";
    Map<String, Integer> paramMap = new HashMap<>();
    paramMap.put("id", id);
    RowMapper<Vote> rm = BeanPropertyRowMapper.newInstance(Vote.class);
    RowMapper<VoteNums> rr = BeanPropertyRowMapper.newInstance(VoteNums.class);
    Vote vote = jdbcTemplate.queryForObject(sql, paramMap, rm);
    Map<String, Integer> anotherMap = new HashMap<>();
    anotherMap.put("vote_item_id", id);
    List<VoteNums> query = jdbcTemplate.query(anotherSql, anotherMap, rr);
    vote.setVoteNums(query);
    return vote;

  }


  @Override
  public void deleteVoteById(Integer id) {
    String anotherSql = "delete from vote_detail  where vote_item_id=:vote_item_id";
    var sql = "delete from vote_item where id=:id";
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", id);
    Map<String, Object> voteItemMap = new HashMap<String, Object>();
    voteItemMap.put("vote_item_id", id);
    jdbcTemplate.update(anotherSql, voteItemMap);
    jdbcTemplate.update(sql, map);
  }

  @Override
  public void addVote(Vote vote) {
    String sql = "insert into vote_item(title,description,single) values(:title,:description,1)";
    String anotherSql = "insert into vote_detail(nums,vote_item_id,item) values(0,:vote_item_id,:item)";
    Map<String, Object> paramMap = new HashMap<>();
    paramMap.put("title", vote.getTitle());
    paramMap.put("description", vote.getDescription());
    KeyHolder keyHolder = new GeneratedKeyHolder();
    SqlParameterSource paramSource = new MapSqlParameterSource(paramMap);
    int update = jdbcTemplate.update(sql, paramSource, keyHolder);
    int key = keyHolder.getKey().intValue();
    List<Map<String, Object>> batchValues = new LinkedList<>();
    for (int i = 0; i < vote.getVoteNums().size(); i++) {
      Map<String, Object> anotherMap = new HashMap<>();
      anotherMap.put("vote_item_id", key);
      anotherMap.put("item", vote.getVoteNums().get(i).getItem());
      batchValues.add(anotherMap);
    }
    jdbcTemplate.batchUpdate(anotherSql, batchValues.toArray(new Map[0]));


  }
}
